<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\common\model;

use think\db\exception\DbException;
use think\Exception;

class Config extends BaseModel
{
    public static $cacheTag = 'sys_config';

    protected array $allowModifyFields = ['sort', 'value'];

    protected array $fields = ['id', 'value', 'name' => 'label'];

    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->query->order('sort', 'desc');
    }

    /**
     * 删除数据
     * @param $ids
     * @return bool
     */
    public function del($ids): bool
    {
        $pk    = $this->getPk();
        try {
            $data = $this->where($pk, 'in', $ids)->select();
        } catch (DbException $e) {
            $this->setError($e->getMessage());
            return false;
        }
        $count = 0;
        $this->startTrans();
        try {
            foreach ($data as $row) {
                if ($row->allow_del === 1) {
                    $count += $row->delete();
                }
            }
            $this->commit();
        } catch (Exception $e) {
            $this->rollback();
            $this->setError($e->getMessage());
        }
        return $count > 0;
    }
}